package com.raising.modules.sys.controller;

import com.raising.StaticParam;
import com.raising.framework.controller.BaseController;
import com.raising.framework.entity.ResultVo;
import com.raising.framework.interceptor.Page;
import com.raising.modules.sys.entity.SysTipVo;
import com.raising.modules.sys.service.SysTipService;
import com.raising.modules.sys.service.UserService;
import com.raising.modules.sys.utils.DictUtils;
import com.raising.utils.JStringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;

/**
 * 提示控制器
 * @author gaoy
 */
@Controller
@RequestMapping("${adminPath}/sys/tip")
public class SysTipController extends BaseController {

	@Autowired
    SysTipService sysTipService;

    @Autowired
    UserService userService;

	/**
	 * 进入提示列表页
	 * @param sysTipVo
	 * @param model
	 * @return
	 */
	@RequiresPermissions("sys:tip:view")
	@RequestMapping(value = "/list")
	public String list(SysTipVo sysTipVo,Model model,Page<SysTipVo> page) throws Exception{
        List<SysTipVo> sysTipVoList = sysTipService.getList(null);
        model.addAttribute("tipList", sysTipVoList);
        model.addAttribute("parentId",sysTipVo.getParentId());
        olog("进入提示管理页面");
        return "sys/tip/list";
	}

    /**
     * 获取列表数据
     * @param sysTipVo
     * @param model
     * @return
     */
    @RequiresPermissions("sys:tip:view")
    @RequestMapping(value = "/getPage.json", method = RequestMethod.POST)
    public @ResponseBody ResultVo getPage(SysTipVo sysTipVo, Model model, Page<SysTipVo> page) throws Exception{
        //初始化加载第一个
        String id="1";
        if (JStringUtils.isNotBlank(sysTipVo.getName()) && id.equals(sysTipVo.getId())){
            sysTipVo.setId("");
        }
        if(JStringUtils.isBlank(sysTipVo.getParentId())){
            sysTipVo.setParentId("0");
        }
        page.setEntity(sysTipVo);
        return ResultVo.success().data(sysTipService.getPage(page));
    }

    /**
     * 跳转到新增页
     * @param sysTipVo
     * @param model
     * @return
     */
    @RequiresPermissions("sys:tip:edit")
    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public String create(SysTipVo sysTipVo, Model model) {
        model.addAttribute("sysTipVo", sysTipVo);
        //是否可用
        model.addAttribute("yn", DictUtils.getDictList("DIM_YN"));
        return "sys/tip/add";
    }

    /**
     * 跳转到修改页
     * @param sysTipVo
     * @param model
     * @return
     */
    @RequiresPermissions("sys:tip:edit")
    @RequestMapping(value = "/update", method = RequestMethod.GET)
    public String update(SysTipVo sysTipVo, Model model) throws Exception{
        model.addAttribute("tipList", sysTipService.getList(sysTipVo));
        model.addAttribute("sysTipVo", sysTipVo);
     // 是否可用
        model.addAttribute("yn",DictUtils.getDictList("DIM_YN"));
        return "sys/tip/edit";
    }

    /**
     * 修改
     * @param sysTipVo
     * @param redirectAttributes
     * @return
     */
    @RequiresPermissions("sys:tip:edit")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(SysTipVo sysTipVo, RedirectAttributes redirectAttributes) throws Exception{
        sysTipService.save(sysTipVo);
        addMessage(redirectAttributes, "修改成功");
        olog("编辑提示信息["+sysTipVo.getName()+"]",sysTipVo.toString());
        return "redirect:" + adminPath + "/sys/tip/list?parentId="+sysTipVo.getParentId();
    }

    /**
     * 逻辑删除
     * @param sysTipVo
     * @param redirectAttributes
     * @return
     */
    @RequiresPermissions("sys:tip:delete")
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public String delete(SysTipVo sysTipVo, RedirectAttributes redirectAttributes) throws Exception{
        //将此数据状态改为无效
        sysTipVo.setStatus(StaticParam.INVALID.toString());
        sysTipService.delete(sysTipVo.getId());
        addMessage(redirectAttributes, "删除成功");
        olog("删除提示["+sysTipVo.getName()+"]",sysTipVo.toString());
        return "redirect:" + adminPath + "/sys/tip/list?parentId="+sysTipVo.getParentId();
    }

}
